#!/bin/bash
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Aux functions
#

SCRIPT_DIR="$(realpath "${0}")"
SCRIPT_DIR="$(dirname "${SCRIPT_DIR}")"
cd "${SCRIPT_DIR}"

if [[ -f "${SCRIPT_DIR}/extras/install-deps-extras.sh" ]]
then
    source "${SCRIPT_DIR}/extras/install-deps-extras.sh"
fi

function print_help
{
    HELP_MESSAGE="
    It is the bootstrap script for Panda on Ubuntu 18.04 or 20.04.

    This script installs all necessary packages for building and testing Panda
    in your local environment, given that your environment is Ubuntu 18.04 or 20.04.
    (detected with the contents of /etc/os-release).

    The script should run with superuser privileges.

    EXAMPLE

    $ ./scripts/install-deps-ubuntu --help
    $ ./scripts/install-deps-ubuntu --install=x86 --install=arm-all --install=dev

    or

    $ ./scripts/install-deps-ubuntu -h
    $ ./scripts/install-deps-ubuntu -i=x86 -i=arm-all -i=dev

    SYNOPSIS

    $0 [OPTIONS]

    OPTIONS

    --help              | -h              Show this message and exit.

    --install=dev       | -i=dev          Install tools needed for development.

    --install=arm-dev   | -i=arm-dev      Install ARM64-hosted tools needed for development.

    --install=arm-all   | -i=arm-all      Install extra packages for cross-compiling for AArch32 and AArch64.

    --install=x86       | -i=x86          Install extra packages for cross-compiling for x86.

    --install=windows   | -i=windows      Install extra packages for cross-compiling for Windows.
    "

    if [[ -n "${EXTRA_OPTIONS}" ]]
    then
        HELP_MESSAGE="${HELP_MESSAGE}${ADDITIONAL_OPTIONS_HELP}"
    fi


    HELP_MESSAGE="${HELP_MESSAGE}
    CAVEAT

    * Packages for cross-compiling for aarch64 and x86 cannot co-exist, so the
    script (read: apt) will replace any conflicting packages on each run.
    * However, packages for cross-compiling for aarch64 and 32-bit ARM can
    co-exist, so they are in a single 'arm-all' dependency list.
    "

    echo "$HELP_MESSAGE"
}

function install_dep
{
    local fname=$1

    if [[ ! -f "$fname" ]] ; then
        echo "FATAL: Dependency list $fname not found."
        exit 1
    fi

    echo "Processing $fname"
    grep --color=never -o '^[^#]*' "$fname" | xargs apt install -y --no-install-recommends -o Dpkg::Options::="--force-overwrite"
}

#
# Main logic
#

#
# Parse command-line arguments
#

# Set default flag values
INSTALL_DEV=no
INSTALL_CROSS_x86=no
INSTALL_CROSS_WINDOWS=no
INSTALL_ARM_DEV=no
INSTALL_CROSS_ARM_ALL=no
SRC_LIST_STR='# This file is generated automatically by Panda install-deps-ubuntu script. DO NOT EDIT!!!\n'

for i in "$@"
do
    ERROR_ARG=""
    case $i in
    -h|--help)
        print_help
        exit 0
        ;;
    --install-qemu-from-sources)
        INSTALL_QEMU_FROM_SOURCES=yes
        ;;
    -i=*|--install=*)
        FLAG_ARG=${i//[-a-zA-Z0-9]*=/}
        if [[ $FLAG_ARG == "dev" ]] ; then
            if [[ $INSTALL_ARM_DEV == "yes" ]] ; then
                echo "FATAL: Parameter --install=dev excludes --install=arm-dev"
                exit 1
            else
                INSTALL_DEV=yes
            fi
        fi
        if [[ $FLAG_ARG == "x86" ]] ; then
            INSTALL_CROSS_x86=yes
        fi
        if [[ $FLAG_ARG == "arm-all" ]] ; then
            INSTALL_CROSS_ARM_ALL=yes
        fi
        if [[ $FLAG_ARG == "windows" ]] ; then
            INSTALL_CROSS_WINDOWS=yes
        fi
        if [[ $FLAG_ARG == "arm-dev" ]] ; then
            if [[ $INSTALL_DEV == "yes" ]] ; then
                echo "FATAL: Parameter --install=arm-dev excludes --install=dev"
                exit 1
            else
                INSTALL_ARM_DEV=yes
            fi
        fi
      ;;
    *)
        ERROR_ARG="YES"
      ;;
    esac

    if [[ -n "${EXTRA_OPTIONS}" ]]
    then
        extra_parse "${i}"
    fi

    if [[ -n "${ERROR_ARG}" ]]
    then
        echo "Error: Unsupported flag $i" >&2
        exit 1
    fi

done

#
# Check 'sudo' and if script is running on Ubuntu
#

if [[ $(id -u) -ne 0 ]] ; then
    print_help
    echo "!!!"
    echo "FATAL: Please run as root."
    echo "!!!"
    exit 1
fi

#
# Check specific Ubuntu version
#

UBUNTU_NAME=ubuntu-18-04

if [ ! -f /etc/os-release ]; then
    echo "FATAL: /etc/os-release not found. Exiting..."
    exit 1
else
    . /etc/os-release

    if [[ $NAME == "Ubuntu" ]]
    then
        set -x
        apt-get update
        dpkg -l | grep curl  || apt-get -y install curl
        dpkg -l | grep gnupg || apt-get -y install gnupg

        if [[ -n "${EXTRA_OPTIONS}" ]]
        then
            extra_add_repo
        fi

        set +x
    else
        echo "FATAL: Only Ubuntu is supported. This is not. Exiting..."
        exit 1
    fi
fi

if [[ $VERSION_ID == "18.04" ]]; then
    echo "Installing packages for Ubuntu 18.04 LTS."
    UBUNTU_NAME=ubuntu-18-04
elif [[ $VERSION_ID == "20.04" ]]; then
    echo "Installing packages for Ubuntu 20.04 LTS."
    UBUNTU_NAME=ubuntu-20-04
else
    echo "Trying to install packages for Ubuntu with unpinned versions."
fi

set -e

#
# Install dependencies
#

install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-build"

if [[ "x$INSTALL_DEV" == "xyes" ]] ; then
    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-dev"
fi

if [[ "x$INSTALL_ARM_DEV" == "xyes" ]] ; then
    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-arm-dev"
fi

if [[ "x$INSTALL_CROSS_x86" == "xyes" ]] ; then
    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-x86"
fi

if [[ "x$INSTALL_CROSS_WINDOWS" == "xyes" ]] ; then
    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-windows"
fi

if [[ "x$INSTALL_CROSS_ARM_ALL" == "xyes" ]] ; then
    if [[ -z "${EXTRA_OPTIONS}" ]]
    then
        "${SCRIPT_DIR}/install-deps-qemu"
    fi
    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-arm-all"
fi

if [[ -n "${EXTRA_OPTIONS}" ]]
then
    extra_install
fi
